﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WTAppletProjectHTTPS.Models;

namespace WTAppletProjectHTTPS.Controllers
{
    public class GetTaskStatusController : ApiController
    {
        public class GetValue
        {
            public string TaskId { get; set; }//任务ID 
        }
        // POST api/<controller>
        public async Task<HttpResponseMessage> Post([FromBody] GetValue GV)
        {
            ReturnObject RO = new ReturnObject();
            try
            {
                if (ExcelTaskQueue.Tasks.TryGetValue(GV.TaskId, out var task))
                {
                    RO.Status = HttpStatusCode.OK;
                    RO.Message = "OK";
                    RO.Data = task;
                    if (task.Status == Models.TaskStatus.Completed)
                    {
                        ExcelTaskQueue.ClearTask(GV.TaskId);
                    }
                    return Request.CreateResponse(HttpStatusCode.OK, RO);

                }
                RO.Status = HttpStatusCode.NotFound;
                RO.Message = "ERROR";
                RO.Data = NotFound();
                return Request.CreateResponse(HttpStatusCode.NotFound, RO);

            }
            catch (Exception ex)
            {
                RO.Status = HttpStatusCode.NotFound;
                RO.Message = "ERROR"+ex.Message;
                RO.Data = NotFound();
                return Request.CreateResponse(HttpStatusCode.NotFound, RO);
            }            
        }
    }
}